<?php

class FriendFacade {
    
    public static function getFriendsByUserId($userId)
    {
        $listId = FriendList::model()->find('user_id=:userId', array(':userId' => $userId));
        return Friend::model()->findAll('friend_list_id=:listId', array(':listId' => $listId->list_id));        
    }
    
    public static function getLatestFriendsByUser($userId)
    {
        $listId = FriendList::model()->find('user_id=:userId', array(':userId' => $userId));
        return Friend::model()->latest()->findAll('friend_list_id=:listId', array(':listId' => $listId->list_id));        
    }
    
    public static function getFriendsCountByUserId($userId)
    {
        $listId = FriendList::model()->find('user_id=:userId', array(':userId' => $userId));
        return Friend::model()->count('friend_list_id=:listId', array(':listId' => $listId->list_id));
    }
    
    public static function getFriendId($userId, $friendId)
    {
        $listId = FriendList::model()->find('user_id=:userId', array(':userId' => $userId));
        $result = Friend::model()->find('friend_list_id=:listId AND user_id=:friendId', array(':listId' => $listId->list_id, ':friendId' => $friendId));
        return $result;
    }
    
    public static function getFriendList($userId)
    {
        return FriendList::model()->find('user_id=:userId', array(':userId' => $userId));
    }
    
    public static function getWaitFriendsByUserId($userId)
    {
        $result = Friend::model()->findAll('user_id=:userId AND accepted=0', array(':userId' => $userId));
        if ($result)
        {
            foreach ($result as $friendId)
            {
                $list[] = array('user_id' => FriendList::model()->findByPk($friendId->friend_list_id)->user_id);
            }
            return $list;
        }
    }
    
    public static function getWaitFriendsCount($userId)
    {
        return Friend::model()->count('user_id=:userId AND accepted=0', array(':userId' => $userId));        
    }
}
?>
